// pages/goods_list/index.js
import { getCateGoodsList } from '../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryId: '',
    catename: '',
    goodsList: [],
    page: 1,
    pageSize: 10,
    totalRow: 0,
    totalPage: 0,
    refreshTop: false,//下拉刷新-显示与否
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const { cateid, catename } = options
    this.setData({ categoryId: cateid, catename })
    this.getGoodsList(cateid)
  },
  // 获取当前分类下商品列表
  // times是否为下拉刷新
  getGoodsList(cateid, times) {
    const { page, pageSize } = this.data
    const params = {
      categoryId: cateid,
      page,
      pageSize
    }
    getCateGoodsList(params).then(res => {
      const { result = [], totalPage = 0, totalRow = 0 } = res.data
      const { goodsList } = this.data
      //下拉刷新-替换数据；上拉加载-拼接数据
      const finalArr = times ? result : goodsList.concat(result)
      this.setData({ goodsList: finalArr, totalRow, totalPage, refreshTop: false})
    })
  },
  refreshBef() {
    this.setData({ refreshTop: true })
  },

  // 下拉刷新事件
  refresh() {
    this.setData({ page: 1 }, () => {
      this.getGoodsList(this.data.categoryId, true)
    })
  },
  // 滑动到底部触发
  downNext() {
    console.log('dibu')
    const { page, totalPage, categoryId } = this.data
    if (page < totalPage) {
      this.setData({ page: page + 1 }, () => {
        this.getGoodsList(categoryId)
      })
    }
    //否则 是最后一页
  },
  
})